package com.example.flow.dao.impl;


import com.example.flow.dao.FlowProcessDao;
import com.example.flow.dao.mapper.FlowProcessMapper;
import com.example.flow.dao.model.FlowProcess;
import com.example.flow.flow.FlowConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

/**
 * @author tangaq
 * 2023/6/15
 */
@Repository
public class RdsFlowProcessDaoImpl implements FlowProcessDao {
    private final FlowProcessMapper flowProcessMapper;

    public RdsFlowProcessDaoImpl(FlowProcessMapper flowProcessMapper) {
        this.flowProcessMapper = flowProcessMapper;
    }

    @Override
    public FlowProcess findByProcessId(String processId) {
        if (StringUtils.isBlank(processId)) {
            return null;
        }
        FlowProcess process = new FlowProcess();
        process.setProcessId(processId);
        return flowProcessMapper.selectOne(process);
    }

    @Override
    public Integer getLatestProcessVersionByType(String processType) {
        return flowProcessMapper.getLatestProcessVersionByType(processType);
    }

    @Override
    public FlowProcess findLatestByProcessType(String processType) {
        FlowProcess process = new FlowProcess();
        process.setProcessType(processType);
        return flowProcessMapper.selectOne(process);
    }

    @Override
    public void updateUnUseStateByProcessType(String processType) {
        if (StringUtils.isBlank(processType)) {
            return;
        }
        FlowProcess update = new FlowProcess();
        update.setState(FlowConstant.STATE_FINISH);
        update.setCts(null);

        FlowProcess example = new FlowProcess();
        update.setProcessType(processType);
        flowProcessMapper.updateByRecordSelective(update, example);
    }

    @Override
    public void updateUnUseStateByProcessId(String processId) {
        if (StringUtils.isBlank(processId)) {
            return;
        }
        FlowProcess update = new FlowProcess();
        update.setState(FlowConstant.STATE_FINISH);
        update.setCts(null);

        FlowProcess example = new FlowProcess();
        update.setProcessId(processId);
        flowProcessMapper.updateByRecordSelective(update, example);
    }

    @Override
    public void deleteByProcessId(String processId) {
        if (StringUtils.isBlank(processId)) {
            return;
        }
        FlowProcess example = new FlowProcess();
        example.setProcessId(processId);
        flowProcessMapper.delete(example);
    }

    @Override
    public void insert(FlowProcess process) {
        flowProcessMapper.insert(process);
    }
}
